﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuXiaChu.OrderDishes.BLL.SiteConfig;
using BuXiaChu.OrderDishes.BLL.Utility;
using BuXiaChu.OrderDishes.Model;
using System.IO;

namespace BuXiaChu.OrderDishes.BLL.FileStore
{
    public class ImageUploadProvider : FileUploadProviderBase
    {
        /// <summary>
        /// 获取已经发布的url全路径
        /// </summary>
        /// <param name="publishUrlPath">入库的url相对路径</param>
        /// <param name="uploadDate">上传时间</param>
        /// <param name="isProduct">true:产品图片 false:公司相关图片</param>
        /// <returns></returns>
        public static string GetPublishedUrl(string publishUrlPath, DateTime uploadDate, bool isProduct)
        {
            UploadFileEntity entity = new UploadFileEntity(0, "default.bmp", new byte[0]);
            ImageUploadProvider provider = new ImageUploadProvider(entity, isProduct, false);
            return entity.PublishUrl + publishUrlPath;
        }

        public ImageUploadProvider(UploadFileEntity entity, bool isProduct, bool validateMime)
        {
            Init(entity, isProduct, validateMime);
        }

        public ImageUploadProvider(UploadFileEntity entity, bool isProduct)
        {
            Init(entity, isProduct, true);
        }

        private void Init(UploadFileEntity entity, bool isProduct, bool validateMime)
        {
            #region
            _ServiceType = isProduct ? "product" : "company";
            _UploadFileEntity = entity;
            MatchFileServer();
            if (validateMime)
                ValidateFileMime();
            GenerateStorePathAndUrl();
            #endregion
        }

        private void GenerateStorePathAndUrl()
        {
            #region
            var result = string.Format(@"\{0}\{1}{2}", _UploadFileEntity.Id, Guid.NewGuid().ToString(), Path.GetExtension(_UploadFileEntity.ClientFileName));

            _UploadFileEntity.ServerStorePath = _FileServer.RootPath + result;
            _UploadFileEntity.PublishUrl = _FileServer.PublishUrl;
            _UploadFileEntity.PublishUrlPath = result.Replace('\\', '/');
            #endregion
        }
    }
}
